package com.sudokuModel;

import android.util.Log;

/**
 * Created by richard on 02/02/14.
 */

// exemple de grille :
//001700509573024106800501002700295018009400305652800007465080071000159004908007053//
//001700509573024106800501002700295018009400305652800007465080071000159004908007053


public class grilleModele {

       public sudokuCase listeCases[];

       public grilleModele(){
           listeCases = new sudokuCase[81];
           for (int i = 0; i < 81 ; i++ ) {
               listeCases[i]= new sudokuCase();
           }
       }
    public void setTestGrille(){

      String res ="008203500009670408346050702430010059967005001000496203280034067703500904004107020";
      for (int i = 0 ; i < res.length(); i++) {
          int v = Integer.parseInt(res.charAt(i)+"");
          if (v!=0){
              listeCases[i].value = v;
              listeCases[i].isFixed = true;
          }
          else{
              listeCases[i].value = 0;
          }

      }
        setCasePossibleParCase();
    }

    public String getDescription(){
       String res="\n";
        for (int i = 0 ; i < 81; i++) {
            res = res  +listeCases[i].value ;
            if (((i+1)%9==0) &&(i>0))res = res +"\n";
        }
        return res;
    }

    public void setCasePossibleParCase(){
        for (int i = 0 ; i < 81; i++) {
            sudokuCase crt = this.listeCases[i];
            this.setCasePossible(crt,i);
        }
    }

    private void setCasePossible(sudokuCase crt,int i) {
        int tabInfoCase[][]= {
                {0,1,2,9,10,11,18,19,20},
                {3,4,5,12,13,14,21,22,23},
                {6,7,8,15,16,17,24,25,26},
                {27,28,29,36,37,38,45,46,47},
                {30,31,32,39,40,41,48,49,50},
                {33,34,35,42,43,44,51,52,53},
                {54,55,56,63,64,65,72,73,74},
                {57,58,59,66,67,68,75,76,77},
                {60,61,62,69,70,71,78,79,80}
        } ;

        // on regarde les case qui sont sur sa line
        int ligne = (i / 9);
        //int colonne = i - (ligne * 9);
        int colonne = i % 9;
        //parcours des cases de la ligne
        for (int l = ligne*9 ; l < (ligne*9) + 9; l ++) {
            sudokuCase aComparer = listeCases[l];
            if (aComparer.value > 0) {
                crt.possibleValue[aComparer.value-1] = false;
            }
        }
       // Log.e("Possible ligne " + i ,crt.getDescription()+"");
        //parcours des cases de la colonne

        for (int c = colonne ; c < 81 ; c = c +9) {
            sudokuCase aComparer = listeCases[c];
            if (aComparer.value > 0) {
                crt.possibleValue[aComparer.value-1] = false;
            }
        }

        int Y = (ligne )  / 3 ;
        int X = (colonne) / 3;
        int rang = (X +(3*Y));
        //Log.e("ligne colonne groupe ", (X +(3*Y)) + "" );
        //Log.e("Possible colonne et ligne " + i ,crt.getDescription()+"");

        for (int k =0 ; k < 9; k ++) {
            int r = tabInfoCase[rang][k];
            sudokuCase aComparer = listeCases[r];
            if (aComparer.value > 0) {
                crt.possibleValue[aComparer.value-1] = false;
            }
        }

    }


}
